package com.web.pojo.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author: wws
 * @date: 2020/1/13 15:47
 * describe:
 *  json封装体
 *
 * | *状态码* | *含义*                 | *说明*
 * | --------| --------------------- | -------------------------------------------
 * | 200     | OK                    | 请求成功
 * | 400     | BAD REQUEST           | 请求的地址不存在或者包含不支持的参数
 * | 401     | UNAUTHORIZED          | 未授权
 * | 403     | FORBIDDEN             | 被禁止访问
 * | 404     | NOT FOUND             | 请求的资源不存在
 * | 444     | Fail                  | 失败
 * | 422     | Unprocesable entity   | [POST/PUT/PATCH] 当创建一个对象时，发生一个验证错误
 * | 500     | INTERNAL SERVER ERROR | 内部错误
 */
@ApiModel(value = "返回结果json封装体")
public class CommonResult<T> implements Serializable {
    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    @ApiModelProperty(value = "消息")
    private String message;
    @ApiModelProperty(value = "数据")
    private T data;

    public static CommonResult success(String msg) {
        return new CommonResult(200,msg);
    }

    public static <T> CommonResult<T> success(String msg,T data) {
        return new CommonResult(200,msg,data);
    }

    public static CommonResult fail(String msg) {
        return new CommonResult(444,msg);
    }

    public static CommonResult UserNotLogin() {
        return new CommonResult(402,"用户未登录");
    }

    public static CommonResult error(String msg) {
        return new CommonResult(444,msg);
    }
    /**
     * 响应返回结果
     * @param rows 影响行数
     * @return 操作结果
     */
    public static CommonResult insert(int rows) {
        return  rows > 0 ? CommonResult.success("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 响应返回结果
     * @return 操作结果
     */
    public static CommonResult authorize() {
        return  CommonResult.success("授权成功");
    }

    /**
     * 响应返回结果
     * @param rows 影响行数
     * @return 操作结果
     */
    public static CommonResult update(int rows) {
        return  rows > 0 ? CommonResult.success("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 响应返回结果
     * @param rows 影响行数
     * @return 操作结果
     */
    public static CommonResult delete(int rows) {
        return  rows > 0 ? CommonResult.success("删除成功") : CommonResult.fail("删除失败");
    }

    /**
     * 响应返回结果
     * @param data 查询结果
     * @return 结果
     */
    public static <T>CommonResult select(T data) {
        return  new CommonResult(200,"查询成功",data);
    }




    public CommonResult() {
    }

    public CommonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}